//index.js
//获取应用实例
const app = getApp()
const request = require("../../utils/request")
Page({
   data: {
      userInfo: {},
      hasUserInfo: false,
      canIUse: wx.canIUse('button.open-type.getUserInfo'),
      encryptedData: "",
      iv: '',
   },
   //事件处理函数
   // bindViewTap: function () {
   //    wx.navigateTo({
   //       url: '../logs/logs'
   //    })
   // },
   onLoad: function () {

      wx.setNavigationBarTitle({
         title: '会员中心',

      })

      // 获取用户信息
      wx.getSetting({
         success: res => {
            if (res.authSetting['scope.userInfo']) {
               // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
               wx.getUserInfo({
                  success: res => {
                     // console.log(res)

                     let nickName = res.userInfo.nickName
                     let avatarUrl = res.userInfo.avatarUrl
                     this.setData({
                        nickName,
                        avatarUrl,
                        hasUserInfo: true
                     })
                     // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
                     // 所以此处加入 callback 以防止这种情况
                     if (this.userInfoReadyCallback) {
                        this.userInfoReadyCallback(res)
                     }
                  }
               })
            }
         }
      })


   },
   getUserInfo: function (e) {
      // console.log(e.detail)

      let nickName = e.detail.userInfo.nickName
      let avatarUrl = e.detail.userInfo.avatarUrl


      let encryptedData = e.detail.encryptedData
      let iv = e.detail.iv


      // 登录
      wx.login({
         success: res => {
            // 发送 res.code 到后台换取 openId, sessionKey, unionId
            // console.log(res)
            let obj = {
               code: res.code,
               encryptedData: this.data.encryptedData,
               iv: this.data.iv
            }

            request("/fenghao/user/wxapp/register/complex", obj).then(res => {
               // console.log(res);
               wx.login({
                  success: function (res) {
                     // success
                     let code = res.code
                     request(`/fenghao/user/wxapp/login?code=${code}`).then(res => {
                        // console.log(res.data.data)
                        let token = res.data.data.token
                        let openid = res.data.data.openid
                        let uid = res.data.data.uid
                        try {
                           wx.setStorageSync('token', token)
                           wx.setStorageSync('openid', openid)
                           wx.setStorageSync('uid', uid)
                           wx.setStorageSync('nickName', nickName)
                           wx.setStorageSync('avatarUrl', avatarUrl)

                        } catch (e) { }
                     })
                  },

               })
            })

         }
      })

      this.setData({
         userInfo: e.detail.userInfo,
         encryptedData,
         iv,
         nickName,
         avatarUrl,
         hasUserInfo: true
      })
   },


   // 收藏
   shoucang() {
      wx.navigateTo({
         url: './shoucang/shoucang',
      })
   },

   dingd() {
      wx.navigateTo({
         url: '/pages/username/dingd/dingd',
      })
   },
   tasset() {
      wx.navigateTo({
         url: '/pages/username/asset/asset',
      })
   },
   tinteg() {
      wx.navigateTo({
         url: '/pages/username/integ/integ',
      })
   },
   tgrow() {
      wx.navigateTo({
         url: '/pages/username/grow/grow',
      })
   },
   yhmd() {
      wx.navigateTo({
         url: '/pages/username/yhmd/yhmd',
      })
   },
   tfxs() {
      wx.navigateTo({
         url: '/pages/username/fxs/fxs',
      })
   },
   dh() {
      wx.navigateTo({
         url: './dh/dh/dh',
      })
   },
   cz() {
      wx.navigateTo({
         url: './cz/cz/cz',
      })
   },
   sq() {
      wx.navigateTo({
         url: "./shen/shen/shen",
      })
   },
   fp() {
      wx.navigateTo({
         url: './fa/fa/fa',
      })
   },
   shouhuo() {
      wx.navigateTo({
         url: '/pages/username/shouhuo/shouhuo',
      })
   },
   qiandao() {
      wx.navigateTo({
         url: './qiandao/qiandao',
      })
   },
   lingjuan() {
      wx.navigateTo({
         url: './lingjuan/lingjuan',
      })
   },


})